using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.ComponentModel;

using UtilityLibrary.Win32;
using UtilityLibrary.Menus;
using UtilityLibrary.Collections;


namespace UtilityLibrary.CommandBars
{
	#region Enumerations
	public enum ToolBarItemStyle
	{
		DropDownButton = 0,
		PushButton = 1,
		Separator = 2,
		ComboBox = 3
	}
	#endregion
		
	#region ToolBarItem
	/// <summary>
	/// Summary description for ToolBarItem.
	/// </summary>
	[ToolboxItem(false)]
	public class ToolBarItem : Component
	{
		#region Events
		public event EventHandler Changed;
		public event EventHandler Click;
		public event EventHandler DropDown;
		#endregion
		
		#region Class Variables
		Image image = null;
		string text = string.Empty;
		bool enabled = true;
		bool check = false;
		bool visible = true;
		bool dropped = false;
		bool raiseEvents = true;
		Keys sCut = Keys.None;
		ToolBarItemStyle style = ToolBarItemStyle.PushButton;
		string toolTip = null;
		Object tag = null;
		EventHandler clickHandler = null;
		ComboBox comboBox = null;
		// ToolBar index
		int index = -1;
		// ImageList index
		int imageListIndex = -1;
		ToolBarEx parentToolBar = null;
		MenuItemExCollection menuItems = null;
		CommandBarMenu commandBarMenu = null;
		#endregion

		#region Constructors
		public ToolBarItem(ToolBarItemStyle style, ComboBox comboBox)
		{
			Debug.Assert(comboBox != null);
			Debug.Assert(style == ToolBarItemStyle.ComboBox);
            this.style = style;
			this.comboBox = comboBox;
		}

		public ToolBarItem()
		{
			Initialize(null, null, null, Keys.None, null, -1);
		}
	
		public ToolBarItem(string text)
		{
			Initialize(null, text, null, Keys.None, null, -1);
		}
	
		public ToolBarItem(string text, EventHandler clickHandler)
		{
			Initialize(null, text, clickHandler, Keys.None, null, -1);
		}

		public ToolBarItem(string text, EventHandler clickHandler, int imageListIndex)
		{
			Initialize(null, text, clickHandler, Keys.None, null, imageListIndex);
		}
				
		public ToolBarItem(Image image, EventHandler clickHandler)
		{
			
			Initialize(image, null, clickHandler, Keys.None, null, -1);
		}
	
		public ToolBarItem(Image image, string text, EventHandler clickHandler, Keys shortcut)
		{
			Initialize(image, text, clickHandler, shortcut, null, -1);
		}

		public ToolBarItem(string text, EventHandler clickHandler, Keys shortcut, int imageListIndex)
		{
			Initialize(null, text, clickHandler, shortcut, null, imageListIndex);
		}

		public ToolBarItem(Image image, EventHandler clickHandler, Keys shortCut)
		{
			Initialize(image, null, clickHandler, shortCut, null, -1);
		}

		public ToolBarItem(Image image, EventHandler clickHandler, Keys shortCut, string toolTip)
		{
			Initialize(image, null, clickHandler, shortCut, toolTip, -1);
		}

		public ToolBarItem(string text, EventHandler clickHandler, Keys shortCut, string toolTip, int imageListIndex)
		{
			Initialize(null, text, clickHandler, shortCut, toolTip, imageListIndex);
		}

		public ToolBarItem(Image image, string text, EventHandler clickHandler, Keys shortCut, string toolTip)
		{
			Initialize(image, text, clickHandler, shortCut, toolTip, -1);
		}

		private void Initialize(Image image, string text, EventHandler clickHandler, Keys shortCut , string toolTip, int imageListIndex)
		{
			this.image = image;
			this.text = text;
			if ( clickHandler != null )
			{
				this.Click += clickHandler;
				this.clickHandler = clickHandler;
			}
			this.sCut = shortCut;
			this.toolTip = toolTip;
			menuItems = new MenuItemExCollection();
			commandBarMenu = new CommandBarMenu(menuItems);
			this.imageListIndex = imageListIndex;
			
		}
		#endregion

		#region Properties
        internal bool RaiseEvents
		{
			set { raiseEvents = value;}
			get { return raiseEvents; }
		}

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[Editor(typeof(UtilityLibrary.Designers.ToolBarItemMenuItemsEditor), typeof(System.Drawing.Design.UITypeEditor))]
		public MenuItemExCollection MenuItems
		{
			set {
				if ( menuItems != value)
				{
				    menuItems = value; 
					commandBarMenu = new CommandBarMenu(menuItems);
					RaiseChanged();
				}
			}
			get 
			{ 
				return menuItems; 
			}
		}

		public CommandBarMenu ToolBarItemMenu
		{
			set { commandBarMenu = value; }
			get 
			{
                // Make sure to return one that
				// it is actually initialize with
				// the item in the MenuItemExCollection
				// -- In case more MenuItemEx items were added after
				// the construction of the ToolBarItem
				commandBarMenu = new CommandBarMenu(menuItems);
				return commandBarMenu; 
			}
		}

		public ComboBox ComboBox
		{
			set { comboBox = value; }
			get { return comboBox; }
		}
	
		public bool Visible
		{
			set { if ( visible != value ) { visible = value; RaiseChanged();} }
			get { return visible; }
		}

		public object Tag
		{
			// No need to raised a changed event since this does not alter the 
			// state of the ToolbarItem but it is just a convinience for the user
			// to associate an object with the ToolbarItem
			set { tag = value;  }
			get { return tag; }
		}
	
		public Image Image
		{
			set { if ( image != value) { image = value; RaiseChanged();}  }
			get { return image; }
		}
		
		public string Text
		{
			set { if (text != value) { text = value; RaiseChanged(); } }
			get { return text; }
		}
	
		public bool Enabled
		{
			set 
			{
				if (enabled != value) 
				{ 
					enabled = value; 
					if ( ComboBox != null)
						ComboBox.Enabled = value;
					RaiseChanged(); 
			}	}
			get { return enabled; }
		}
	
		public bool Checked
		{
			set { if (check != value) { check = value; RaiseChanged(); } }
			get { return check; }
		}

		public Keys Shortcut
		{
			set { if (sCut != value) { sCut = value; RaiseChanged(); } }
			get { return sCut; }
		}
	
		public ToolBarItemStyle Style
		{
			set { if (style != value) { style = value; RaiseChanged(); } }
			get { return style; }
		}

		public string ToolTip 
		{
			set { if (toolTip != value) { toolTip = value; RaiseChanged(); } }
			get { return toolTip; }
		}

		public int Index
		{
			set { index = value; }
			get { return index; }
		}

		public bool Dropped
		{
			set { dropped = value; }
			get { return dropped; }
		}

		public int ImageListIndex
		{
			set { imageListIndex = value; }
			get { return imageListIndex; }
		}

		public Rectangle ItemRectangle
		{
			get
			{
				// toolBar object must have been setup right before
				// rendering the toolbar by the toolbar itself and for all items
				if ( parentToolBar != null)
				{
					RECT rect = new RECT();
					WindowsAPI.SendMessage(parentToolBar.Handle, (int)ToolBarMessages.TB_GETRECT, index, ref rect);
					return new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
				}
				else return Rectangle.Empty;
			}
		}

		public ToolBarEx ToolBar
		{
			set { 
				parentToolBar = value; 
			}
			get
			{
				return parentToolBar;
			}
		}

		public EventHandler ClickHandler 
		{
			set 
			{ if ( Click != value) 
              { 
				  Click = value;
				  clickHandler = value; 
				  RaiseChanged(); 
			  } 
			}
			get { return clickHandler; }
		}
		#endregion

		#region Implementation	
		internal void RaiseChanged()
		{
			if (Changed != null && raiseEvents ) Changed(this, EventArgs.Empty);
		}

		internal void RaiseClick()
		{
			if (Click != null && raiseEvents ) Click(this, EventArgs.Empty);
		}
	
		internal void RaiseDropDown()
		{
			if (DropDown != null && raiseEvents ) DropDown(this, EventArgs.Empty);
		}
		#endregion
	}
	#endregion
}
